package com.nf.health.app.activity.home.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.nf.health.app.R;
import com.nf.health.app.mydoctor.adapter.GzDoctorAdapter;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.customview.DragListView;
import com.nf.health.app.customview.EmptyView;
import com.nf.health.app.db.DbManager;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.models.DoctorVo;
import com.nf.health.app.models.GroupManagerInfo;
import com.nf.health.app.utils.EmUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * A
 * 医生fragment
 */
public class DoctorFragment extends IBaseFragment {
    String flag = "2";
    @Bind(R.id.consul_listview)
    DragListView consulListview;

    private List<DoctorVo> mlist = new ArrayList<DoctorVo>();
    private GzDoctorAdapter adapterDoctor2;

    private int page = 1;
    private int pageSize = 20;

    int position1;

    private boolean isFistComeIn = true;//限制之第一次进来的时候进行把群信息保存带数据库中

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        addViewFillInRoot(R.layout.fragment_doctor);

        ButterKnife.bind(this, rootView);
        //flag = getArguments().getString("type");
        isFistComeIn = true;
        initData();

        consulListview.setRefreshableAndLoadMoreable(true, true);

        consulListview.setOnRefreshAndLoadMoreListener(new DragListView.onRefreshAndLoadMoreListener() {

            @Override
            public void onRefresh() {
                if (flag.equals("3")) {

                } else {
                    page = 1;
                    initData("attention", "refresh2");
                }
            }

            @Override
            public void onLoadMore() {
                if (flag.equals("3")) {

                } else {
                    page++;
                    initData("attention", "more2");
                }
            }
        });


    }

    private void initData() {
        if (flag.equals("2")) {//关注医生
            initView2();
        } else if (flag.equals("3")) {//加入的群
            initView3();
        }

    }

    private void initView2() {
        provider.requestMyDoctorList("attention", page + "", pageSize + "", "refresh2");
    }

    private void initView3() {
        showToast("加入的群");

    }

    private void initData(String type, String flag) {

        provider.requestMyDoctorList(type, page + "", pageSize + "", flag);
    }

    @SuppressWarnings("unchecked")
    @Override
    public void handleActionSuccess(String actionName, Object object) {
        if ("refresh2".equals(actionName)) {
            consulListview.completeRefresh();
            List<DoctorVo> result = (List<DoctorVo>) object;
            EmUtil.saveDoctorVoAsContact(result);
            mlist.clear();
            if (isFistComeIn == true) {
                saveGroupManagerInfo(result);
                EmUtil.saveDoctorVoAsContact(result);
                isFistComeIn = false;
            }
            if (result.size() <= 0) {
                if (adapterDoctor2 != null) {
                    adapterDoctor2.notifyDataSetChanged();
                    consulListview.setAdapter(adapterDoctor2);
                }
                consulListview.setEmptyView( (new EmptyView(getActivity())).mustCallInitWay(consulListview).setImageViewParams(120,120).setMyRefreshShow(View.GONE).
                        setOnRefreshData(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                page = 1;
                                initData("attention", "refresh2");
                            }
                        }));
            } else {
                mlist.addAll(result);
                if (adapterDoctor2 == null) {
                    adapterDoctor2 = new GzDoctorAdapter(getActivity(), mlist);
                    consulListview.setAdapter(adapterDoctor2);
                } else {
                    adapterDoctor2.notifyDataSetChanged();
                }
            }
        } else if ("more2".equals(actionName)) {
            consulListview.completeLoadMore();
            List<DoctorVo> result = (List<DoctorVo>) object;
            EmUtil.saveDoctorVoAsContact(result);
            if (result.size() == 0) {
                showToast("没有更多数据了");
            } else {
                mlist.addAll(result);
                adapterDoctor2.notifyDataSetChanged();
            }
        } else if (actionName.equals("deleteAttention")) {

            mlist.remove(position1);
            adapterDoctor2.notifyDataSetChanged();

        }

        adapterDoctor2.setOnMyClickListener(new OnMyClickListener() {
            @Override
            public void onClick(int position, String flag) {
                provider.requestCancleFoucsDoctor(flag, "deleteAttention");
                position1 = position;

            }
        });
    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            initData("attention", "refresh2");
        }
    }

    public void onResume() {
        super.onResume();
        page = 1;
        provider.requestMyDoctorList("attention", page + "", pageSize + "", "refresh2");
    }

    /***********
     * 获取该用户参加所有的群组
     **************/
    private void asyncLoadAllGroup() {

    }

    private void saveGroupManagerInfo(final List<DoctorVo> list) {
        final List<GroupManagerInfo> managerlist = new ArrayList<GroupManagerInfo>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (DoctorVo user : list) {// addGroupManagerList
                    GroupManagerInfo model = new GroupManagerInfo();
                    model.setGroupId(user.getDoctorid());
                    model.setUsername(user.getNick());
                    managerlist.add(model);
                }
                DbManager.getInstance().addGroupManagerList(managerlist);
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isFistComeIn = false;
    }
}